package org.inter.fsb.service.impl;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.io.unit.DataUnit;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.inter.fsb.entity.TUserWord;
import org.inter.fsb.entity.TWork;
import org.inter.fsb.entity.User;
import org.inter.fsb.mapper.TWorkMapper;
import org.inter.fsb.service.ITUserWordService;
import org.inter.fsb.service.ITWorkService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.inter.fsb.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author syg
 * @since 2024-04-18
 */
@Transactional(rollbackFor = Exception.class)
@Service
public class TWorkServiceImpl extends ServiceImpl<TWorkMapper, TWork> implements ITWorkService {

    @Autowired
    private ITUserWordService itUserWordService;
    @Autowired
    private IUserService iUserService;

    @Override
    public void qwork(String username, Integer workId) throws Exception {
        TWork work = this.getById(workId);

        if (username.equals(work.getUsername())){
            throw new Exception("这是自己发布的单");
        }

        QueryWrapper<User> userQueryWrapper = new QueryWrapper<>();
        userQueryWrapper.eq("username",username);
        User user = iUserService.getOne(userQueryWrapper);
        if (user != null && work != null){
            TUserWord tUserWord = new TUserWord();
            tUserWord.setJine(work.getJine());
            tUserWord.setUserId(user.getId());
            tUserWord.setName(user.getUsername());
            tUserWord.setUsername(work.getUsername());
            tUserWord.setImages(work.getImages());
            tUserWord.setWorkId(work.getId());
            tUserWord.setDateTime(DateUtil.formatDateTime(new Date()));
            tUserWord.setTitle(work.getTitle());
            tUserWord.setFangshi(work.getQudao());
            tUserWord.setYongjin(work.getYongjin());
            tUserWord.setStatus("未提交");

            QueryWrapper<TUserWord> tUserWordQueryWrapper = new QueryWrapper<>();
            tUserWordQueryWrapper.eq("user_id",tUserWord.getUserId()).eq("work_id",tUserWord.getWorkId());

            List<TUserWord> list = itUserWordService.list(tUserWordQueryWrapper);

            if (work.getGeshu() - 1 >= 0 && list.size() == 0){
                work.setGeshu(work.getGeshu() - 1);
                this.saveOrUpdate(work);
                itUserWordService.saveOrUpdate(tUserWord);
            }else {
                throw new Exception("次数用完");
            }

        }else {
            throw new Exception("检测未知访问");
        }
    }

    @Override
    public void twork(String zhengming, Integer userWorkId) throws Exception {
        TUserWord byId = itUserWordService.getById(userWorkId);
        if (byId != null){
            byId.setStatus("待审核");
            byId.setZhengming(zhengming);
            itUserWordService.saveOrUpdate(byId);
        }else {
            throw new Exception("无数据");
        }

    }

    @Override
    public List listWork(String username, Integer status) throws Exception {

        QueryWrapper<User> userQueryWrapper = new QueryWrapper<>();
        userQueryWrapper.eq("username",username);
        User user = iUserService.getOne(userQueryWrapper);
        Integer userId = user.getId();

        //我发布的
        if (status == 1){
            QueryWrapper<TWork> tWorkQueryWrapper = new QueryWrapper<>();
            tWorkQueryWrapper.eq("user_id",userId);
            List<TWork> list = this.list(tWorkQueryWrapper);
            return  list;
        }
        // 已完成
        if (status == 2){
            QueryWrapper<TUserWord> tUserWordQueryWrapper = new QueryWrapper<>();
            tUserWordQueryWrapper.eq("user_id",userId);
            tUserWordQueryWrapper.ne("status","已完成");
            List<TUserWord> list = itUserWordService.list(tUserWordQueryWrapper);
            return list;
        }
        //3 待审核  或  4已审核
        if (status == 3 || status == 4){

            QueryWrapper<TWork> tWorkQueryWrapper = new QueryWrapper<>();
            tWorkQueryWrapper.eq("user_id",userId);
            List<TWork> list = this.list(tWorkQueryWrapper);
            List<Integer> collect = list.stream().map(i -> i.getId()).collect(Collectors.toList());

            if (collect.size() == 0){
                return null;
            }

            QueryWrapper<TUserWord> tUserWordQueryWrapper = new QueryWrapper<>();
            tUserWordQueryWrapper.in("work_id",collect);
            if (status == 3){
                tUserWordQueryWrapper.eq("status","未审核");
            }else {
                tUserWordQueryWrapper.ne("status","未审核");
            }

            List<TUserWord> list1 = itUserWordService.list(tUserWordQueryWrapper);
            return list1;
        }


        return null;
    }

    @Override
    public void shen(Integer uwId, Integer status) throws Exception {

        TUserWord userWord = itUserWordService.getById(uwId);

        if (userWord != null && !"通过".equals(userWord.getStatus())){

            TWork tWork = this.getById(userWord.getWorkId());

            //拒绝
            if (status == 0){
                userWord.setStatus("拒绝");
                tWork.setGeshu(tWork.getGeshu() + 1);
                this.saveOrUpdate(tWork);
            }
            //通过
            if (status == 1){
                userWord.setStatus("通过");
                User user = iUserService.getById(userWord.getUserId());
                user.setJine(user.getJine().add(tWork.getYongjin()));
                iUserService.saveOrUpdate(user);
            }

            itUserWordService.saveOrUpdate(userWord);
        }else {
            throw new Exception("状态不可更改");
        }


    }
}
